package com.sxt.system.controller;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.sxt.system.constast.Constast;
import com.sxt.system.domain.User;
import com.sxt.system.service.IUserService;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.MD5Utils;
import com.sxt.system.utils.ResultObj;
import com.sxt.system.vo.UserVO;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-08
 */
@RestController
@RequestMapping("user")
public class UserController {
	
	private Log log = LogFactory.getLog(UserController.class);

	@Autowired 
	private IUserService userService;
	
	/**
	 * 全查询
	 */
	@RequestMapping("loadAllUser")
	public DataGridView loadAllUser(UserVO userVO) {
		return this.userService.loadAllUser(userVO);
	}
	
	/**
	 * 加载最大的排序码
	 */
	@RequestMapping("loadUserMaxOrderNum")
	public DataGridView loadUserMaxOrderNum() {
		Integer maxOrder = this.userService.queryUserMaxOrderNum();
		return new DataGridView(maxOrder+1);
	}
	
	/**
	 * 	根据部门ID查询员工
	 */
	@RequestMapping("queryUserByDeptId")
	public DataGridView queryUserByDeptId(Integer deptid) {
		return new DataGridView(this.userService.queryUserByDeptId(deptid));
	}
	
	/**
	 * 	添加用户
	 */
	@RequestMapping("addUser")
	public ResultObj addUser(User user) {
		user.setHiredate(new Date());//设置入职时间
		user.setType(Constast.USER_TYPE_NORMAL);//用户类型
		user.setImgpath(Constast.USER_DEFAULT_IMAGE);
		user.setSalt(MD5Utils.getSalt());
		user.setPwd(MD5Utils.md5(Constast.USER_DEFAULT_PWD, user.getSalt(), 2));
		try {
			userService.addUser(user);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			log.error("添加失败");
			return ResultObj.ADD_ERROR;
		}
	}
	
	/**
	 * 根据用户ID查询用户对象
	 */
	@RequestMapping("loadUserByUserId")
	public DataGridView loadUserByUserId(Integer userId) {
		User user = this.userService.getById(userId);
		return new DataGridView(user);
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("updateUser")
	public ResultObj updateUser(User user) {
		try {
			userService.updateUser(user);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			log.error("修改失败");
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("deleteUser")
	public ResultObj deleteUser(Integer id) {
		try {
			userService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 	批量删除
	 */
	@RequestMapping("batchDeleteUser")
	public ResultObj batchDeleteUser(Integer [] ids) {
		try {
			if(ids==null||ids.length==0) {
				log.error("参数不能为空");
				return ResultObj.DELETE_ERROR;
			}
			Collection<Serializable> idList = new ArrayList<Serializable>();
			for (Integer  integer  : ids) {
				idList.add(integer);
			}
			this.userService.removeByIds(idList);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 	重置密码
	 */
	@RequestMapping("resetUserPwd")
	public ResultObj resetUserPwd(Integer id) {
		try {
			userService.resetUserPwd(id);
			return ResultObj.RESET_SUCCESS;
		} catch (Exception e) {
			log.error("重置失败");
			return ResultObj.RESET_ERROR;
		}
	}
	
	/**
	 * 保存角色和用户之间的关系  sys_role_user
	 */
	@RequestMapping("saveUserRole")
	public ResultObj saveUserRole(Integer userId,Integer[] rids) {
		try {
			this.userService.saveUserRole(userId,rids);
			return ResultObj.DISPATCH_SUCCESS;
		} catch (Exception e) {
			log.error("分配失败");
			return ResultObj.DISPATCH_ERROR;
		}
	}
	
	/**
	 * 修改密码
	 */
	@RequestMapping("changePwd")
	public ResultObj changePwd(String oldPwd,String newPwd,String secondPwd,HttpSession session) {
		//获取session中的用户
		User user = (User) session.getAttribute("user");
		if(!StringUtils.equals(newPwd, secondPwd)) {
			return ResultObj.CHANGE_SECOND_ERROR;
		}
		if(!StringUtils.equals(user.getPwd(), MD5Utils.md5(oldPwd, user.getSalt(), 2))) {
			return ResultObj.CHANGE_OLD_ERROR;
		}
		try {
			userService.changePwd(user.getId(),newPwd);
			return ResultObj.CHANGE_SUCCESS;
		} catch (Exception e) {
			log.error("修改失败");
			return ResultObj.CHANGE_ERROR;
		}
		
	}
	
	/**
	 * 	修改个人信息
	 */
	@RequestMapping("changeUser")
	public ResultObj changeUser(User user) {
		try {
			this.userService.changeUser(user);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			log.error("修改失败");
			return ResultObj.UPDATE_ERROR;
		}
	}
}


